package cn.ldf.keepaccounts.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.ldf.keepaccounts.dao.UsersDao;
import cn.ldf.keepaccounts.entity.pojo.Users;
import cn.ldf.keepaccounts.entity.vo.WxUserVO;
import cn.ldf.keepaccounts.exception.ServerException;
import cn.ldf.keepaccounts.service.UsersService;
import cn.ldf.keepaccounts.utils.ExceptionUtil;
import cn.ldf.keepaccounts.utils.WxRequest;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 用户表(Users)表服务实现类
 *
 * @author 清峰
 * @since 2025-02-17 10:16:42
 */
@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {

    @Resource
    private WxRequest wxRequest;

    @Override
    public Users insertUsers(Users users) {
        //是否用户已经存在
        boolean isExists = this.lambdaQuery().eq(Users::getOpenId, users.getOpenId()).exists();
        ExceptionUtil.throwMsg(() -> isExists, "用户已经存在");
        this.save(users);
        return users;
    }

    @Override
    public Users selectOneByOpenId(String openId) {
        return this.lambdaQuery().eq(Users::getOpenId, openId)
                .select(Users::getId, Users::getOpenId,
                        Users::getUnionId, Users::getUnionId,
                        Users::getNickname, Users::getNickname,
                        Users::getScore, Users::getWinningCount,
                        Users::getGamesCount, Users::getGamesCount,
                        Users::getWinningRate, Users::getWinningRate
                )
                .one();
    }

    @Override
    public Users getByOpenId(String openId) {
        return this.lambdaQuery().eq(Users::getOpenId, openId).one();
    }

    @Override
    public Boolean isExistByOpenId(String openId) {
        return this.lambdaQuery().eq(Users::getOpenId, openId).exists();
    }



    @Override
    public List<Users> getListAvatarByOpenIds(List<String> openIds) {
        List<Users> usersList = this.lambdaQuery().in(Users::getOpenId, openIds).select(Users::getOpenId, Users::getNickname, Users::getAvatar,Users::getContentType).list();
        return ObjUtil.isNotEmpty(usersList) ? usersList : Collections.emptyList();
    }

    @Override
    public List<Users> getListByOpenId(List<String> openIds) {
        List<Users> usersList = this.lambdaQuery().in(Users::getOpenId, openIds).list();
        return ObjUtil.isNotEmpty(usersList) ? usersList : Collections.emptyList();
    }

    @Override
    public WxUserVO getOpenIdByCode(String code) {
        WxUserVO wxUserVO = wxRequest.getOpenIdByCode(code);
        ExceptionUtil.throwMsg(()-> ObjUtil.isEmpty(wxUserVO), "获取微信用户信息失败");
        return wxUserVO;
    }

    @Override
    public Boolean uploadBaseAvatar(byte[] avatarByte,String contentType, String openId, String nickname) {
        ExceptionUtil.throwMsg(() -> !isExistByOpenId(openId), "不存在该用户");
        LambdaUpdateChainWrapper<Users> wrapper = this.lambdaUpdate();
        if (ObjUtil.isEmpty(avatarByte)) {
            wrapper.set(Users::getNickname, nickname);
        }
        wrapper.set(Users::getAvatar, avatarByte)
                .set(Users::getContentType, contentType)
                .eq(Users::getOpenId, openId).update();
        return wrapper.update();
    }

}

